using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DotNet.Utilities
{
    public class webopera2
    {
        private WebBrowser myweb;

        public delegate void openpage(string url);
        public openpage openpagedelegate;
        public delegate void waitload();
        public waitload waitloaddelegate;
        public delegate HtmlElement getelement(string name);
        public getelement getelementdelegate;
        public delegate HtmlDocument getdoc();
        public getdoc getdocdelegate;
        public delegate HtmlElementCollection getByTagName(string name);
        public getByTagName getByTagNamedelegate;
        public delegate object getdomdocment();
        public getdomdocment getdomdocmentdelegate;

        public webopera2(WebBrowser web)
        {
            myweb = web;
        }

        public void OpenPage(string url)
        {
            if (!myweb.InvokeRequired)
            {
                myweb.Navigate(url);
            }
            else
            {
                openpagedelegate = new openpage(OpenPage);
                myweb.Invoke(openpagedelegate, new object[] { url});
            }
            WaitLoad();
        }

        public void WaitLoad()
        {
            DateTime now = DateTime.Now;
            if (myweb.InvokeRequired)
            {
                waitloaddelegate = new waitload(WaitLoad);
                myweb.Invoke(waitloaddelegate);
            }
            else
            {
                try
                {
                    while (myweb.ReadyState != WebBrowserReadyState.Complete)
                    {
                        Application.DoEvents();
                        TimeSpan ts = DateTime.Now.Subtract(now);
                        if (ts.Seconds > 10)
                        {
                            break;
                        }
                    }
                }
                catch
                {
                    return;
                }
            }
        }

        public HtmlDocument GetDoc()
        {
            if (myweb.InvokeRequired)
            {
                getdocdelegate = new getdoc(GetDoc);
                return (HtmlDocument)myweb.Invoke(getdocdelegate);
            }
            return myweb.Document;
        }

        public HtmlElement GetEl(string name)
        {
            DateTime time = DateTime.Now;

            HtmlElement El = GetElement(name);
            while (El == null)
            {
                El = GetElement(name);
                Application.DoEvents();
                TimeSpan ts = DateTime.Now.Subtract(time);
                if (ts.Seconds > 5)
                {
                    break ;
                }
               
            }
            return El;
        }

        public HtmlElement GetElement(string name)
        {
            if (myweb.InvokeRequired)
            {
                getelementdelegate = new getelement(GetElement);
                return (HtmlElement)myweb.Invoke(getelementdelegate, new object[] {name });
            }
            return (HtmlElement)myweb.Document.All[name];
        }

        public void SetValue(string name,string vl)
        {
            HtmlElement el = GetEl(name);
            if (el != null)
            {
                el.SetAttribute("value", vl);
                
            }
        }

        public void ElementClick(string name)
        {
            HtmlElement el = GetEl(name);
            if (el != null)
            {
                el.InvokeMember("click");
            }
        }

        public string GetHtml()
        {
            HtmlElement body = GetDoc().Body;
            while (body == null)
            {
                body = GetDoc().Body;
            }
            string html = body.InnerHtml;
            return html;
        }


        public object GetDomdocment()
        {
            if (myweb.InvokeRequired)
            {
                getdomdocmentdelegate = new getdomdocment(GetDomdocment);
                return (object)myweb.Invoke(getdomdocmentdelegate);
            }
            return myweb.Document.DomDocument;
        }

        public HtmlElementCollection GetByTagName(string name)
        {
            if (myweb.InvokeRequired)
            {
                getByTagNamedelegate = new getByTagName(GetByTagName);
                return  (HtmlElementCollection)myweb.Invoke(getByTagNamedelegate, new object[] {name });
            }
            return GetDoc().GetElementsByTagName(name);
        }

        public void executeOnChangeHandler(mshtml.IHTMLElement element, string eventname)
        {
            //mshtml.IHTMLElement e = element as mshtml.IHTMLElement;
            //mshtml.IHTMLDocument4 doc4 = e.document as mshtml.IHTMLDocument4;
            //object dummy = null;
            //object eventObj = doc4.CreateEventObject(ref dummy);

            //mshtml.HTMLSelectElementClass se = element as mshtml.HTMLSelectElementClass;
            //se.FireEvent(eventname, ref eventObj);
        }

        public void ExecJs(string functionname, string parms)
        {
            mshtml.IHTMLDocument2 currentDoc = (mshtml.IHTMLDocument2)myweb.Document.DomDocument;
            mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)currentDoc.parentWindow;
            win.execScript(functionname + "(" + parms + ")", "javascript");
        }


        public void Cancelalertandconfirm()
        {
            mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)myweb.Document.Window.DomWindow;

            string s = @"function confirm() {";
            s += @"return true;";
            s += @"}";

            s += @"function alert() {}";

            win.execScript(s, "javascript");

        }
    }
}
